/**
 * 2011-11-13下午8:39:22
 */
package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Properties;

/**
 * @author Baicai 资源文件读取工具类，用来读取配置文件
 */
public class ReadProperties {

	/**
	 * 读取配置文件的方法，获取key，得到结果并返回
	 * 
	 * @param keyString
	 * @return String
	 */
	public static String readFile(String keyString) {

		String resultString = "";
		FileInputStream fileInputStream = null;

		try {
			URI uri = ReadProperties.class.getResource("").toURI();//获取类加载器的路径
			File propertiesFile = new File(uri);//取到路径
			String string = propertiesFile.getAbsolutePath()
					+ System.getProperty("file.separator") + "db.properties";//得到绝对路径
			
			File file = new File(string);// 配置文件位置
			fileInputStream = new FileInputStream(file);
			Properties properties = new Properties();
			properties.load(fileInputStream);

			resultString = properties.getProperty(keyString);// 得到结果字符串

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();// 流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return resultString;
	}

}
